﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
namespace EDTS.Utils
{
    public static class StringExtension
    {
        /// <summary>
        /// 取汉字拼音简写
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string GetPyString(this string str)
        {
            string ret = "";
            foreach(char c in str)
            {
                if((int)c >= 33 && (int)c <=126)
                {//字母和符号原样保留
                    ret += c.ToString();
                }
                else
                {//累加拼音声母
                    ret += GetPYChar(c.ToString());
                }
            }
            return ret;
        }
        /// <summary>
        /// 取单个汉字拼音
        /// </summary>
        /// <param name="hz"></param>
        /// <returns></returns>
        private static string GetPYChar(string hz)
        {
            #region 纠错
            //var errstr = "己";
            //var py = "j";
            //var index = errstr.IndexOf(hz);
            //if (index >= 0)
            //    return py[index].ToString();
            #endregion

            byte[] array = new byte[2];
            array = System.Text.Encoding.Default.GetBytes(hz);
            int i = (short)(array[0] - '\0') * 256 + ((short)(array[1] - '\0'));

            if (i < 0xB0A1) return "*";
            if (i < 0xB0C5) return "a";
            if (i < 0xB2C1) return "b";
            if (i < 0xB4EE) return "c";
            if (i < 0xB6EA) return "d";
            if (i < 0xB7A2) return "e";
            if (i < 0xB8C1) return "f";
            if (i < 0xB9FE) return "g";
            if (i < 0xBBF7) return "h";
            if (i < 0xBFA6) return "j";
            if (i < 0xC0AC) return "k";
            if (i < 0xC2E8) return "l";
            if (i < 0xC4C3) return "m";
            if (i < 0xC5B6) return "n";
            if (i < 0xC5BE) return "o";
            if (i < 0xC6DA) return "p";
            if (i < 0xC8BB) return "q";
            if (i < 0xC8F6) return "r";
            if (i < 0xCBFA) return "s";
            if (i < 0xCDDA) return "t";
            if (i < 0xCEF4) return "w";
            if (i < 0xD1B9) return "x";
            if (i < 0xD4D1) return "y";
            if (i < 0xD7FA) return "z";

            return "*";
        }
        /// <summary>
        /// 去掉小数的后置0
        /// </summary>
        /// <param name="str"></param>
        /// <param name="length">保留小数位数</param>
        /// <returns></returns>
        public static string RemoveLastZero(this string str, int length = 0)
        {
            var pi = str.IndexOf(".");
            if (pi < 0) return str;
            var l = str.Length;
            if (l - pi - 1 <= length) return str;
            var a = str.Substring(0, pi);
            var b = str.Substring(pi + 1, l - pi - 1);
            while (b.Length > length)
            {
                var z = b.Substring(b.Length - 1, 1);
                if (z != "0") break;
                b = b.Substring(0, b.Length - 1);
            }

            var ret = a;
            if (length > 0 || b.Length > 0) ret += ".";
            ret += b;
            return ret;
        }
        /// <summary>
        /// 身份证号的验证
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool CheckIDNo(this string str)
        {
            str = str.ToUpper();
            if (Regex.IsMatch(str, @"^\d{17}(\d|X|x)$"))
            {
                int[] weight = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };    //十七位数字本体码权重
                char[] validate = { '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2' };    //mod11,对应校验码字符值
                int sum = 0;
                int mode = 0;
                var id17 = str.Substring(0, 17);
                for (int i = 0; i <17; i++)
                    sum = sum + Convert.ToInt32(id17.Substring(i,1)) * weight[i];
                mode = sum % 11;
                var c = str[17];
                return c == validate[mode];
            }
            return false;
        }
    }
}
